package com.scau.ums.service.imp.info;

import com.scau.ums.mapper.InfoMapper;
import com.scau.ums.model.Info;
import com.scau.ums.model.User;
import com.scau.ums.service.SendInfo;
import com.scau.ums.service.UserInfo;
import com.scau.ums.service.factory.UserInfoImpFactory;
import com.scau.ums.service.imp.mail.SendMailImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author: 莫伟杰
 * date： 2019/8/5 12:23
 */
@Service
@EnableAsync
public abstract class SendInfoImp implements SendInfo {

    protected InfoMapper mapper;
    protected DataSourceTransactionManager transactionManager;
    protected UserInfoImpFactory userInfoImpFactory;
    protected SendMailImp sendMailImp;

    @Autowired
    public void setUserInfoImpFactory(UserInfoImpFactory userInfoImpFactory) {
        this.userInfoImpFactory = userInfoImpFactory;
    }

    @Autowired
    public void setMapper(InfoMapper mapper) {
        this.mapper = mapper;
    }

    @Autowired
    public void setTransactionManager(DataSourceTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Autowired
    public void setSendMailImp(SendMailImp sendMailImp) {
        this.sendMailImp = sendMailImp;
    }

    //标准创建一条信息，参数（标题，发送者ID，接受者ID，接受者类型，发送内容）
    public Info createInfo(String title, String sendId, String recId,String receiverType, String content)
    {
        Info info = new Info();
        info.setId(0);
        info.setTitle(title);
        info.setSenderId(sendId);
        info.setReceiverId(recId);
        info.setRecieverType(receiverType);
        info.setContent(content);
        info.setState(UNCHECKED);
        info.setDate(new Date());
        return info;
    }

    //标准创建一组信息，参数（标题，发送者ID，一组接受者ID，对应接受者的类型，发送内容）
    public List<Info> createInfoList(String title, String sendId, List<String> recIds,List<String> receiverType , String content)
    {
        List<Info> infoList = new ArrayList<>();
        for(int i=0;i<recIds.size();i++)
        {
            createInfo(title,sendId,recIds.get(i),receiverType.get(i),content);
        }
        return infoList;
    }

    @Override
    public void sendInfo(Info info) {
        //缺少发送者和接受者拒绝发送
        if(info.getReceiverId()==null||info.getSenderId()==null) return;
        if(mapper.insert(info)==1)
        {
            //发送邮件
            System.out.println("sendInfo执行了");
            try {
                UserInfo userInfo = userInfoImpFactory.getUserInfoImp(User.getUserTypeByChinese(info.getRecieverType()));
                sendMailImp.sendMail(userInfo.getUser(info.getReceiverId()).getMail(),info.getTitle(),info.getContent());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendInfos(List<Info> infoList) {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try
        {
            for (Info info:infoList) {
                //缺少发送者和接受者拒绝发送
                sendInfo(info);
            }
            transactionManager.commit(status);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            transactionManager.rollback(status);
        }
    }
}
